package model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.print.attribute.standard.DateTimeAtProcessing;

import dao.RommDAO;

public class Hotel {
	private ListEmployee listEmp = new ListEmployee();
	private ListRoom listRoom = new ListRoom();
	private ListCustomer listcustomer = new ListCustomer();
	private ListPayment listpayment = new ListPayment();
	private ListReservation listreservation = new ListReservation();
	private ListCategory listCategory = new ListCategory();

	private Employee empOnline = null;

	public Hotel() {
	}

	public void connect() {
		listRoom.connect();
		listcustomer.connect();
		listpayment.connect();
		listreservation.connect();
		listEmp.connect();
		listCategory.connect();

	}

	public ListEmployee getListEmp() {
		return listEmp;
	}

	public ListRoom getListRoom() {
		return listRoom;
	}

	public ListCustomer getListcustomer() {
		return listcustomer;
	}

	public void setListcustomer(ListCustomer listcustomer) {
		this.listcustomer = listcustomer;
	}

	public ListReservation getListReservation() {
		return listreservation;
	}

	public ListCategory getListCategory() {
		return listCategory;
	}

	public Employee getEmpOnline() {
		return empOnline;
	}

	public void addEmployee(int id, String fname, String lname, String uname,
			String pwd, int pos) {
		listEmp.addEmployee(id, fname, lname, uname, pwd, pos);
	}

	public void addEmployee(Employee e) {
		listEmp.addEmployee(e);
	}

	// ------------------------------------------------------------------------------------
	// login
	public boolean employeeLogin(String uname, String pwd) {
		boolean re = false;
		int a = listEmp.employeeLogin(uname, pwd);
		System.out.println("a= " + a);
		if (empOnline != null)
			a = -1;
		if (a != -1) {
			empOnline = listEmp.getEmployeeByID(a);
			System.out.println("Login success");
			re = true;
		} else
			System.out.println("Login failed");
		return re;
	}

	// logout
	public void employeeLogout() {
		if (empOnline != null) {
			empOnline = null;
			System.out.println("Logout success");
		}
	}

	// ------------------------------------------------------------------------------------
	// add room
	public void addRoom(String category, long price, String description) {
		listRoom.addRoom(category, price, description);
	}

	public void addRoom(Room r) {
		listRoom.addRoom(r);
	}

	public void deleteRoom(int id) {
		listRoom.deleteroom(id);
	}

	// search available room
	public ListRoom searchRoomAvailable(String type, long priceMin,
			long priceMax) {
		return listRoom.searchRoomAvailable(type, priceMin, priceMax);
	}

	public ListRoom searchRoomByID(int id) {
		return listRoom.searchRoomByID(id);
	}

	public ListRoom searchRoomByType(String type) {
		return listRoom.searchRoomByType(type);
	}

	public ListRoom searchRoomByStatus(int status) {
		return listRoom.searchRoomByStatus(status);
	}

	public ListRoom searchRoomByPrice(long priceMin, long priceMax) {
		return listRoom.searchRoomByPrice(priceMin, priceMax);
	}

	public boolean set_statusRoom(int room_id, int status) {
		return listRoom.set_statusRoom(room_id, status);
	}

	public Vector findby_Category(String category_id) {
		return listRoom.findby_Category(category_id);
	}

	// customer
	public boolean addCustomer(Customer r) {
		return listcustomer.addcustomer(r);
	}

	// ------------------------------------------------------------------------------------
	// make reservation
	public boolean make_reservation(int reservation_id, int customer_id,
			int room_id, Date expected_arrive_date,
			Date expected_departure_date, int status, String note,
			int employee_reservation) {
		// // status_reservation : 1-cancel ; 2 -used; 3/ waiting booking..
		// status_room : 1 - ready, 2- booked, 3- serving
		listRoom.set_statusRoom(room_id, 2);

		return listreservation.addreservation(reservation_id, customer_id,
				room_id, expected_arrive_date, expected_departure_date, 3,
				note, employee_reservation);
	}

	public boolean make_reservation(Reservation re) {
		// // status_reservation : 1-cancel ; 2 -used; 3/ waiting booking..
		// status_room : 1 - ready, 2- booked, 3- serving
		listRoom.set_statusRoom(re.getRoom_id(), 2);
		return listreservation.addreservation(re);
	}

	public boolean make_reservationTest(Reservation re) {
		// // status_reservation : 1-cancel ; 2 -used; 3/ waiting booking..
		// status_room : 1 - ready, 2- booked, 3- serving
		listRoom.set_statusRoom(re.getRoom_id(), 2);
		return listreservation.addreservationTest(re);
	}

	// cancel reservation
	public boolean cancel_reservation(int reservation_id) {
		// // status_reservation : 1-cancel ; 2 -used; 3/ waiting booking..
		// status_room : 1 - ready, 2- booked, 3- serving
		Reservation reser = this.get_ReservationbyId(reservation_id);
		listRoom.set_statusRoom(reser.getRoom_id(), 1);

		return listreservation.delete_reservation(reservation_id);
	}

	// change reservation
	public boolean change_reservation(int reservation_id, int customer_id,
			int room_id, Date expected_arrive_date,
			Date expected_departure_date, int status, String note,
			int employee_reservation) {
		// // status_reservation : 1-cancel ; 2 -used; 3/ waiting booking..
		// status_room : 1 - ready, 2- booked, 3- serving
		Reservation reser = this.get_ReservationbyId(reservation_id);
		Room room = listRoom.getRoomByID(reser.getRoom_id());
		if (room.getID() != room_id) {
			// old
			listRoom.set_statusRoom(reser.getRoom_id(), 1);
			// new
			listRoom.set_statusRoom(room_id, 2);
		}
		Reservation reser_new = new Reservation(reservation_id, customer_id,
				room_id, expected_arrive_date, expected_departure_date, status,
				note, employee_reservation);
		return listreservation.updatereservation(reser_new);
	}

	// find reservation
	public Reservation get_ReservationbyId(int reservation_id) {
		return listreservation.search_reservation(reservation_id);
	}

	// find reservation
	public Vector getAllReservation() {
		return listreservation.getReservations();
	}

	// check in
	public boolean check_in(int customer_id, int reservation_id,
			int employee_check_in) {
		boolean re = false;
		Reservation reser = this.getListReservation().search_reservation(
				reservation_id);
		// status_reservation : 1-cancel ; 2 -used; 3/ waiting booking..
		// status_room : 1 - ready, 2- booked, 3- serving
		if (reser != null) {
			// ------------------------------------------------
			int room_id = reser.getRoom_id();
			Room room = listRoom.getRoomByID(room_id);
			listRoom.getRoomByID(room_id).setStatus(2);

			// get date now
			// DateFormat dateFormat = new
			// SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			// get current date time with Date(
			Date expected_arrive_date = new Date();

			// --------------
			reser.setExpected_arrive_date(expected_arrive_date);
			reser.setEmployee_check_in(employee_check_in);
			reser.setStatus(2);
			listreservation.updatereservation(reser);
			// ------------------------------------------------
			re = true;
		}
		return re;
	}

	// long Amount
	// Single: .(11USD/1 ngay)
	// Double: (15USD/1 ngay)
	// Triple: ( .(20USD/1 ngay)

	public long payment_Amount(Date arr_date, Date depart_date) {
		long re = 0;

		return re;
	}

	// check out
	public Payment check_out(int customer_id, int reservation_id,
			int employee_check_out) throws ParseException {

		Reservation reser = this.getListReservation().search_reservation(
				reservation_id);
		// status_reservation : 1-cancel ; 2 -used; 3/ waiting booking..
		// status_room : 1 - ready, 2- booked, 3- serving
		if (reser != null) {
			reser.setEmployee_check_out(employee_check_out);
			reser.setStatus(1);
			// get date now
			Date expected_departure_date = new Date();
			// --------------
			reser.setExpected_departure_date(expected_departure_date);

			int room_id = reser.getRoom_id();
			Room room = listRoom.getRoomByID(room_id);
			listRoom.getRoomByID(room_id).setStatus(0);

			// create payment
			long payment_Amount = 100;

			SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");

			long d1 = expected_departure_date.getTime();
			long d2 = reser.getExpected_arrive_date().getTime();

			payment_Amount = (Math.abs((d1 - d2) / (1000 * 60 * 60 * 24)))
					* room.getPrice();

			Payment payment = new Payment(customer_id, employee_check_out,
					payment_Amount, reservation_id);

			listpayment.addPayment(payment);

		}

		return new Payment();
	}

	public void setEmpOnline(Employee empOnline) {
		this.empOnline = empOnline;
	}

	// ---------------------------------------------------
	// /add
	public boolean check_customer(int customer_id) {
		Customer cu = listcustomer.getCustomerByID(customer_id);
		if (cu == null) {
			return false;
		}
		return true;
	}

}
